package com.luo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.luo.dao.MemberDao;
import com.luo.dao.OrderDao;
import com.luo.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zoro
 * @date 2021年09月02
 */
@Service(interfaceClass = ReportService.class)
@Transactional(readOnly = true)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> map = new HashMap<>(12);

        try {
            // reportDate（当前时间）--String
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            map.put("reportDate", today);

            // todayNewMember（今日新增会员数） -> number
            Integer todayNewMember = memberDao.getTodayNewMember(today);
            map.put("todayNewMember", todayNewMember);

            // totalMember（总会员数） -> number
            Integer totalMember = memberDao.getTotalMember();
            map.put("totalMember", totalMember);

            // thisWeekNewMember（本周新增会员数） -> number
            // 2：本周（周一）
            String weekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            // 3：本周（周日）
            String weekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
            Integer thisWeekNewMember = memberDao.startEndSelect(weekMonday, weekSunday);
            map.put("thisWeekNewMember", thisWeekNewMember);

            // thisMonthNewMember（本月新增会员数） -> number
            // 4：本月（1号）
            String monthFirst = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            // 5：本月（31号）
            String monthLast = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());
            Integer thisMonthNewMember = memberDao.startEndSelect(monthFirst, monthLast);
            map.put("thisMonthNewMember", thisMonthNewMember);

            // todayOrderNumber（今日预约数） -> number
            Integer todayOrderNumber = orderDao.todayOrderNumber(today);
            map.put("todayOrderNumber", todayOrderNumber);

            // todayVisitsNumber（今日出游数） -> number
            Integer todayVisitsNumber = orderDao.todayVisitsNumber(today);
            map.put("todayVisitsNumber", todayVisitsNumber);

            // thisWeekOrderNumber（本周预约数） -> number
            Integer thisWeekOrderNumber = orderDao.startEndSelectNo(weekMonday, weekSunday);
            map.put("thisWeekOrderNumber", thisWeekOrderNumber);

            // thisWeekVisitsNumber（本周出游数） -> number
            Integer thisWeekVisitsNumber = orderDao.startEndSelectYes(weekMonday, weekSunday);
            map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);

            // thisMonthOrderNumber（本月预约数） -> number
            Integer thisMonthOrderNumber = orderDao.startEndSelectNo(monthFirst, monthLast);
            map.put("thisMonthOrderNumber", thisMonthOrderNumber);

            // thisMonthVisitsNumber（本月出游数） -> number
            Integer thisMonthVisitsNumber = orderDao.startEndSelectYes(monthFirst, monthLast);
            map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

            // hotSetmeal（热门套餐（取前4）） -> List<Setmeal>
            List<Map<String, Object>> hotSetmeal = orderDao.hotSetMeal();
            map.put("hotSetmeal", hotSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
